package com.timeshare.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.timeshare.common.Result;
import com.timeshare.dto.ReviewRequest;
import com.timeshare.entity.Review;
import com.timeshare.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 评价控制器
 */
@Tag(name = "评价管理")
@RestController
@RequestMapping("/review")
public class ReviewController {
    
    private final ReviewService reviewService;
    
    public ReviewController(ReviewService reviewService) {
        this.reviewService = reviewService;
    }
    
    @Operation(summary = "创建评价")
    @PostMapping("/create")
    public Result<?> createReview(Authentication authentication, @Valid @RequestBody ReviewRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            reviewService.createReview(userId, request);
            return Result.success("评价成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取用户收到的评价")
    @GetMapping("/user/{userId}")
    public Result<Page<Review>> getUserReviews(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Review> reviews = reviewService.getUserReviews(userId, page, size);
            return Result.success(reviews);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

